<?
class formatter{
	public function __construct(){
	}
	
	static function number($value){
		#print("<p>LALLA: $value</p>");
		$value=preg_replace("/[^\\d\\.]/i","",$value);
		#print("<p>LALLA: $value</p>");
		return($value);
	}
	
	static function currency($value,$dp=2,$thousands_sep=",",$curr_symbol="\$"){
		$value=preg_replace("/[^\\d\\.]/i","",$value);
		if(!is_numeric($value)){
			$value=0;
		}
		$value=$curr_symbol.number_format($value,$dp,".",$thousands_sep);
		return($value);
	}
	
	static function cmsUserData($data=""){
		$result=$data;
		$result=str_replace("\n","<br>",$result);
		return($result);
	}
	
	static function secondsToTime($seconds){
		$mu=($seconds<0)?-1:1;
		$t=abs($seconds);
		$h=floor($t/3600);
		$t%=3600;
		$m=floor($t/60);
		$s=floor($t%60);
		if(strlen($h)<2){
			$h="0".$h;
		}
		return(substr("0".($h*$mu),-2).":".substr("0".$m,-2).":".substr("0".$s,-2));
	}
	
	static function timeToSeconds($time=NULL){
		$ar=preg_split("/\:/",$time);
		$hrs=$mins=$secs=0;
		if(count($ar)>2){
			$hrs=(int)$ar[count($ar)-3];
			$mins=(int)$ar[count($ar)-2];
			$secs=(int)$ar[count($ar)-1];
		}elseif(count($ar)>1){
			$mins=(int)$ar[count($ar)-2];
			$secs=(int)$ar[count($ar)-1];
		}elseif(count($ar)>0){
			$secs=(int)$ar[count($ar)-1];
		}
		return(($hrs*3600)+($mins*60)+$secs);
	}
	
	static function auDateToUniversal($date=NULL,$strtotime=true,$nullIfEmpty=true){
		$result=$date;
		if($date!==NULL&&strlen($date)>0){
			$ar=preg_split("/[\/\.\-\s]/",$date);
			if(count($ar)>2){
				$result=$ar[2]."-".$ar[1]."-".$ar[0];
				if(count($ar)>3){
					$result.=" ".$ar[3];
				}
				if($strtotime){
					$result=strtotime($result);
				}
			}
		}elseif($nullIfEmpty){
			$result=NULL;
		}
		return($result);
	}

	// Converts $title to Title Case, and returns the result.
	static function TCase($text){
		$result="";
		if($text!==NULL){
			// Our array of 'small words' which shouldn't be capitalised if
			// they aren't the first word. Add your own words to taste.
			$smallwordsarray=array('of','a','the','and','an','or','nor','but','is','if','then','else','when', 'at','from','by','on','off','for','in','out','over','to','into','with');

			// Split the string into separate words
			$words=explode(' ',$text);
			foreach($words as $key=>$word){
				// If this word is the first, or it's not one of our small words, capitalise it with ucwords().
				if($key==0||!in_array($word,$smallwordsarray)){
					$words[$key]=ucwords($word);
				}
			}
			// Join the words back into a string
			$result=implode(' ',$words);
		}
		return($result);
	}
	//}
	/*
		Calculates and Formats the difference in dates.
		Formatting
			NULL/Empty: full output (i.e. 2 years, 3 months, 1 week, 2 days).
			n=Nearest, rounded (i.e. if its 2 years and 5 days, result=2 years)
			c=compact (i.e. 1Y2M3W2D4H)
	*/
	static function dateDifference($date1=NULL,$date2=NULL,$format=NULL){
		$result=0;
		$diff=0;
		$tmp=0;
		$date1=($date1===NULL)?time():$date1;
		$date2=($date2===NULL)?time():$date2;
		if($date2<$date1){
			$tmp=$date2;
			$date2=$date1;
			$date1=$tmp;
		}
		$diff=$date2-$date1;
		
		return($result);
	}
	
	/*
		Returns a JavaScript formatted string
	*/
	static function jsText($content=""){
		return(preg_replace('`[\r\n]`','\n',addslashes($content)));
	}
};
?>